package com.yinp.proapp.fragment.bottompage;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.yinp.proapp.adapter.ComViewHolder;
import com.yinp.proapp.adapter.CommonAdapter;
import com.yinp.proapp.adapter.SingleViewHolder;
import com.yinp.proapp.base.fragment.AppBaseFragment;
import com.yinp.proapp.bean.BaseBean;
import com.yinp.proapp.databinding.FragmentRecreationBinding;
import com.yinp.proapp.databinding.ItemRecreationListBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: yinp
 * @Date: 2021/9/8
 * @Description:娱乐，游戏
 */
public class RecreationFragment extends AppBaseFragment<FragmentRecreationBinding> {
    private CommonAdapter<BaseBean> adapter;
    private List<BaseBean> dataList = new ArrayList<>();

    @Override
    protected void initViews(View view) {
        initRecycler();
    }

    private void initRecycler() {
        dataList.add(new BaseBean("贪吃蛇", "com.yinp.proapp.module.game.tanchishe.GSGameContentActivity", null,
                "一个自己写的利用canvas画出贪吃蛇的游戏，目前未完成"));
        dataList.add(new BaseBean("四色地图游戏", "com.yinp.proapp.module.game.fourcolormap.FourColorMapActivity", null,
                "一个自己通过svg地图实现的四色地图游戏"));
        adapter = new CommonAdapter<BaseBean>(getContext(), dataList) {
            @Override
            protected SingleViewHolder setComViewHolder(View view, int viewType, ViewGroup parent) {
                ItemRecreationListBinding binding = ItemRecreationListBinding.inflate(mInflater, parent, false);
                SingleViewHolder viewHolder = new SingleViewHolder(binding);
                binding.stvStart.setOnClickListener(v -> {
                    String url = dataList.get(viewHolder.getAbsoluteAdapterPosition()).getUrl();
                    if (!TextUtils.isEmpty(url)) {
                        Intent intent = new Intent();
                        intent.setClassName(mContext, url);
                        goToActivity(intent);
                    }
                });
                return viewHolder;
            }

            @Override
            public void onBindItem(SingleViewHolder holder, int position, BaseBean item) {
                ItemRecreationListBinding binding = (ItemRecreationListBinding) holder.getBinding();
                binding.tvTitle.setText(item.getTitle());
                binding.tvIntroduce.setText(item.getContent());
            }
        };
        bd.rvList.setLayoutManager(new LinearLayoutManager(getContext()));
        bd.rvList.setHasFixedSize(true);
        bd.rvList.setAdapter(adapter);
    }
}
